<?php
namespace app\admin\controller\rbac;

use app\admin\BaseController;
use app\admin\business\rbac\Auth as AuthBusiness;
use app\admin\validate\rbac\Login as ValidateLogin;
use app\admin\business\rbac\Rbac;
use app\admin\business\rbac\Auth;
use think\facade\View;

class Login extends BaseController {

    public function index(){
        $arr_admin_info=session('admin');
        $str_view="";
        if($arr_admin_info){
            //获取菜单数据
            $arr_auth_data=array();
            $obj_auth_business = new Auth();
            $obj_auth_business->getAllMenuTree(0,$arr_admin_info['auth_ids'],$arr_auth_data);
//            Rbac::getAllMenuTree(0,$arr_admin_info['auth_ids'],$arr_auth_data);
            View::assign('arr_auth_data',$arr_auth_data);
            View::assign('admin_info',$arr_admin_info);
            $str_view="home";
        }
        return view($str_view);
    }

    public function adminLogin(){
        $obj_login_validate=new ValidateLogin();
        $arr_data=[
            'account'=>$this->request->param('account'),
            'pwd'=>$this->request->param('pwd'),
            'vercode'=>$this->request->param('vercode'),
        ];
        if(!$obj_login_validate->check($arr_data)){
            return output_data(array(),'paramserror',$obj_login_validate->getError());
        }
        $obj_business_auth = new Auth();
        $arr_admin_info = $obj_business_auth->login($this->request->param('account','','trim'), $this->request->param('pwd','','trim'));
        if(empty($arr_admin_info)){
            $str_code_name='admin-login-accountpwderror';
        }else{
            if($arr_admin_info['is_lock']==2){
                return output_data(array(),'admin-login-islock');
            }
            //获取角色
            $obj_business_auth = new AuthBusiness();
            switch ($arr_admin_info['type']){
                case 0:
                    $arr_admin_info['auth_ids'] = 0;
                    break;
                case 1:
                    $arr_admin_info['auth_ids'] = 1;
                    break;
                default:
                    $arr_admin_info['auth_ids']=$obj_business_auth->getAuthIdsByAdminId($arr_admin_info['id']);
            }
            //获取权限路由
            $arr_auth_urls=$obj_business_auth->getAuthUrls($arr_admin_info['id']);
            $arr_admin_info['auth_urls']=$arr_auth_urls;
            session('admin',$arr_admin_info);
            $str_code_name='success';
        }
        return output_data($arr_admin_info,$str_code_name);
    }

    public function adminLogOut(){
        session('admin',null);
        return view('index');
    }
}